const Router = require('koa-router');
let router = new Router();
const mongoose = require('mongoose');
router.post('/login', async (ctx) => {
    // get请求ctx.request.query(格式化) queryString(未格式化)
    // post请求ctx.request.body
    // 获取model
    const User = mongoose.model('user');
    // 接收请求
    await User
        .find({
            userName: ctx.request.body['userName']
        }).select({
            userName: 1,password:1,city:1,name:1,age:1,
            realName: 1, userId: 1, roleId: 1, orgId:1,gender:1
        })
        .then((res) => {
            if(res.length){
                // console.log(res[0])
                if (ctx.request.body['password'] === res[0].password) {
                    ctx.body = {
                        code: 200,
                        data: Object.assign(res[0], {token: 'a123456'}),
                        message: '登陆成功',
                    };
                } else {
                    ctx.body = {
                        code: 300,
                        data:'',
                        message: '密码错误',
                    };
                }
            } else{
                ctx.body = {
                    code: 300,
                    data:'',
                    message: '用户不存在',
                };
            }
            
        })
        .catch((err) => {
            ctx.body = {
                code: 500,
                data: '',
                message: err,
            };
        });
});

module.exports = router;